﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControl : MonoBehaviour
{

    private float gSpeed;
    public Vector3 gGodPosition;//上帝视角的位置
    public Transform gCameraTramsform;//相机
    public bool gIsGod;//上帝视角
    public Vector3 gCurrentPosition;//记录第一视角的位置
    public Vector3 gCurrentAngle;//记录第一视角的角度
    // Use this for initialization
    void Start()
    {
        gIsGod = false;
        gSpeed = 50;
        gGodPosition = new Vector3(0, 1000, 0);
        gCameraTramsform = Camera.main.transform;
        gCameraTramsform.position = new Vector3(-500, 100, 0);
    }

    // Update is called once per frame
    void Update()
    {
        if (!gIsGod)
        {
            if (Input.GetMouseButton(1))//移动，0,代表左键，1代表鼠标右键
            {
                float pX = Input.GetAxis("Mouse X");
                float pY = Input.GetAxis("Mouse Y");
                gCameraTramsform.Rotate(Vector3.up, pX);//绕Y轴旋转
                gCameraTramsform.Rotate(Vector3.right, -pY);//绕X轴旋转,疑问，为什么是绕X轴旋转？
            }

            if (Input.GetKey(KeyCode.W))//向前移动
            {
                Vector3 pVector3d = new Vector3(gCameraTramsform.forward.x, 0, gCameraTramsform.forward.z);
                gCameraTramsform.Translate(pVector3d * gSpeed * Time.deltaTime);
            }
            if (Input.GetKey(KeyCode.S))//向后移动
            {
                Vector3 pVector3d = new Vector3(gCameraTramsform.forward.x, 0, gCameraTramsform.forward.z);
                gCameraTramsform.Translate(-pVector3d * gSpeed * Time.deltaTime);
            }
        }
        if (Input.GetKeyDown(KeyCode.Q))//通过Q键切换第一视觉及上帝
        {
            if(gIsGod ==true )
            {
                gCameraTramsform.position = gCurrentPosition;//回复第一视觉的位置
                gCameraTramsform.eulerAngles = gCurrentAngle;//回复第一视觉的角度
                gIsGod = false;
            }
            else
            {
                gCurrentPosition = gCameraTramsform.position;//记录第一视觉的位置
                gCurrentAngle = gCameraTramsform.eulerAngles;//记录第一视觉的角度
                gCameraTramsform.eulerAngles = new Vector3(90, 0, 0);
                gCameraTramsform.position = gGodPosition;
                gIsGod = true;
            }

        }

    }
}
